/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */
package sg.edu.nus.iss.vmcs.customer;

import sg.edu.nus.iss.vmcs.util.VMCSException;

/**
 * This control object is for handling the dispense drink use case.
 * 
 * @author Team SE16T5E
 * @version 1.0 2008-10-01
 * @editedby A0119952X - Singleton Design Pattern
 */
public class DispenseController {
	private Facade facade = Facade.getInstance();

    private static DispenseController instance;
    private DispenseController() {

    }

    public static DispenseController getInstance() {
        if (instance == null) {
            instance = new DispenseController();
        }
        return instance;
    }

    //	/**
//	 * This constructor creates an instance of the object.
//	 *
//	 * @param txCtrl
//	 *            the Transaction Controller
//	 */
//	public DispenseController(TransactionController txCtrl) {
//		this.txCtrl = txCtrl;
//		facade = Facade.getInstance();
//	}

	/**
	 * This method updates the whole Drink Selection Box with current names,
	 * stocks and prices.
	 */
	public void updateDrinkPanel() {
		facade.updateDrinkPanel();
	}

	/**
	 * This method will be used to activate or deactivate (as indicated through
	 * a parameter) the Drink Selection Box so that transactions can continue or
	 * will be disallowed.
	 * 
	 * @param allow
	 *            TRUE to activate, FALSE to deactivate the Drink Selection Box.
	 */
	public void allowSelection(boolean allow) {
		facade.allowSelection(allow);
	}

	/**
	 * This method will be used to instruct the Can Collection Box to remove the
	 * drinks can shape or drink brand name from being displayed.
	 */
	public void ResetCan() {
		TransactionController.getInstance().getCustomerPanel().resetCan();
	}

	/**
	 * This method will be used to dispense a drink&#46; It will: <br>
	 * 1- Instruct the Drinks Store to dispense the drink&#46; It will also
	 * instruct the Can Collection Box to display a can shape&#46; <br>
	 * 2- Instruct the Store Controller to update the Drink Store Display on the
	 * Machinery Simulator Panel&#46; <br>
	 * 3- In case of fault detection, it will send a "fault detected" response
	 * to the Transaction Controller&#46;
	 * 
	 * @param selectedBrand
	 *            the selected brand&#46;
	 */
	public boolean dispenseDrink(int selectedBrand) {
		try {
			facade.dispenseDrink(selectedBrand);
		} catch (VMCSException ex) {
            TransactionController.getInstance().terminateFault();
			return false;
		}
		return true;
	}
}